#include "wagontimer.h"

WagonTimer::WagonTimer(QObject *_parent)
{
    connect(this, SIGNAL(timeout()), this, SLOT(inc()));
    this->setInterval(10);
    this->setObjectName("wagontimer");
    this->setParent(_parent);
    this->start();
    action = 0; // initialize the wagon "pointed" right
    isMoving = false;
}

// Handle making the wagon move left and right
void WagonTimer::inc()
{   
    switch(action) // Pick which animation action to perform
    {
    case 0: // handle moving right
        {
            if (isMoving == true) // move wagon to the right
            {
                if (this->object->pos().x() + this->object->width() < ((HelicopterForm*)(this->parent()))->width())
                {
                    QPoint pos = this->object->pos();
                    this->object->setGeometry(pos.x() + 3, pos.y(), this->object->width(), this->object->height());
                }
            }
        }
        break;
    case 1: // handle moving left
        {
            if (isMoving == true) // move wagon to the right
            {
                if (this->object->pos().x() > 0)
                {
                    QPoint pos = this->object->pos();
                    this->object->setGeometry(pos.x() - 3, pos.y(), this->object->width(), this->object->height());
                }
            }
        }
        break;
    default:
        break;
    }
}

// set up which action "inc" will perform
void WagonTimer::SetAction(int act)
{
    action = act;
}

// set up which action "inc" will perform
int WagonTimer::GetAction()
{
    return action;
}

// move it right
void WagonTimer::MoveRight()
{
    ((Wagon*)(this->object))->animMovie->setPaused(false);
    action = 0;
    isMoving = true;
}

// stop it
void WagonTimer::StopMove()
{
    isMoving = false;
    ((Wagon*)(this->object))->animMovie->setPaused(true);
}

// move it left
void WagonTimer::MoveLeft()
{
    ((Wagon*)(this->object))->animMovie->setPaused(false);
    action = 1;
    isMoving = true;
}
